<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>权限管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="layui-elem-field layuimini-search">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="searchFrm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">权限名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                          <label class="layui-form-label">权限模块</label>
                          <div class="layui-input-inline">
                              <input type="text" name="module" autocomplete="off" class="layui-input">
                          </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">权限类型</label>
                            <div class="layui-input-inline">
                                <input type="text" name="operationType" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">权限URI</label>
                            <div class="layui-input-inline">
                                <input type="text" name="permissionUri" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item" style="margin-top: 5px;">
                            <div class="layui-block" style="text-align: center;">
                                <a class="layui-btn " lay-submit=""  lay-filter="data-search-btn"><label class="layui-icon layui-icon-search"></label>搜索</a>
                                <a class="layui-btn layui-btn-warm" onclick="javascript:$('#searchFrm')[0].reset()" lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolBar">
          <div class="layui-btn-group">
            <a class="layui-btn layui-btn-sm data-count-add" lay-event="add">添加</a>
            <a class="layui-btn layui-btn-sm data-count-delete" lay-event="delete">删除</a>
          </div>
		    </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="tableBar">
          <a class="layui-btn layui-btn-xs layui-btn-warm data-count-update" lay-event="update">修改</a>
        </script>

        <!--添加和修改的弹出层开始-->
        <div id="addOrUpdateDiv" style="display: none;padding: 0.3125rem">
          <form method="post" class="layui-form" lay-filter="dataFrm" id="dataFrm">
            <div class="layui-form-item">
              <label class="layui-form-label">权限名称</label>
              <div class="layui-input-block">
                <input type="hidden" name="id" />
                <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入权限名称" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">权限模块</label>
              <div class="layui-input-block">
                <input type="hidden" name="id" />
                <input type="text" name="module" lay-verify="required" autocomplete="off" placeholder="请输入权限模块" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">权限类型</label>
              <div class="layui-input-block">
                <input type="hidden" name="id" />
                <input type="text" name="operationType" lay-verify="required" autocomplete="off" placeholder="请输入权限类型" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">权限URI</label>
              <div class="layui-input-block">
                <input type="hidden" name="id" />
                <input type="text" name="permissionUri" lay-verify="required" autocomplete="off" placeholder="请输入权限URI" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block" style="text-align: right;">
                <a class="layui-btn " lay-submit=""  lay-filter="doSubmit"><label class="layui-icon layui-icon-release"></label>提交</a>
                <a class="layui-btn layui-btn-warm" onclick="javascript:$('#dataFrm')[0].reset()" lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
              </div>
            </div>
          </form>
        </div>
        <!--添加和修改的弹出层结束-->

    </div>
</div>

<script src="/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="/lib/common/common.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
      var $ = layui.jquery,
          form = layui.form,
          table = layui.table;

      var tableIns = table.render({
          elem: '#currentTableId',
          url: api + '/permission/getByPage',
          method: 'post',
          contentType: "application/json; charset=utf-8",
          toolbar:"#toolBar",
          cellMinWidth: true,
          parseData: function(res){ //res 即为原始返回的数据
              return {
                  "code": res.code, //解析接口状态
                  "msg": res.msg, //解析提示文本
                  "count": res.data.total, //解析数据长度
                  "data": res.data.records //解析数据列表
              };
          },
          response:{ // 必须配置，否则不加载数据
              statusName:'code', //规定返回的状态码字段为code
              statusCode:200 //规定成功的状态码味200
          },
          cols: [[
              {type: "checkbox", width: 50, fixed: "left"},
              {type: "numbers", width: 40, title: '序号', fixed: "left"},
              {field: 'id', width: 80, title: 'ID', hide: true},
              {field: 'name', minWidth: 100, title: '权限名称'},
              {field: 'module', minWidth: 200, title: '权限模块'},
              {field: 'operationType', minWidth: 100, title: '权限类型'},
              {field: 'permissionUri', minWidth: 120, title: '权限URI'},
              {title: '操作', templet: '#tableBar', fixed: "right", align: "center"}
          ]],
          limits: [10, 15, 20, 25, 5],
          limit: 15,
          page: true
      });

      // 监听搜索操作
      form.on('submit(data-search-btn)', function (data) {
          //执行搜索重载
          table.reload('currentTableId', {
            page: {
              curr: 1
            },
            where: {
              queryParams: {
                  "name": data.field.name,
                  "module": data.field.module,
                  "operationType": data.field.operationType,
                  "permissionUri": data.field.permissionUri
              }
            },
            async: true
          }, 'data');

          return false;
      });

      //提交数据
      form.on("submit(doSubmit)",function(data){
        let params = {
          "id": data.field.id,
          "name": data.field.name,
          "module": data.field.module,
          "operationType": data.field.operationType,
          "permissionUri": data.field.permissionUri
        };
        $.ajax({
          url: url,
          method: formState === FORM_ADD? "post" : "put",
          async: true,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          data: JSON.stringify(params),
          success:function(res){
            layer.msg(res.msg);
            tableIns.reload();
          },
          error:function(){
            layer.msg("操作失败");
          }
        });
        layer.close(mainIndex);
        return false;
      })
 
      //监听表头的事件
      table.on('toolbar(currentTableFilter)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
          case 'delete':
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            if(data.length > 0) {
                layer.confirm('真的删除么', function (index) {
                  var arr = [];
                  //遍历
                  data.forEach(function(item){
                      arr.push(item.id)
                  });
                  $.ajax({
                      url: api + "/permission/deleteByIds",
                      method: "delete",
                      contentType: "application/json; charset=utf-8",
                      data: JSON.stringify(arr),
                      success:function(res){
                          layer.msg(res.msg);
                          tableIns.reload();
                      },
                      error:function(){
                          layer.msg("删除失败");
                      }
                  });
                  layer.close(index);
              });
            }
          break;
          case 'add':
            openAddPermission();
          break;
        };
      });

      //监听行数据的事件
      table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'update'){
				  openUpdatePermission(data);
		    } else if (obj.event === 'show'){
				  showPermission(data);
			  }
      });

      //打开添加的弹层
      var mainIndex;
      var url;  //动态的url
      var formState;
      function openAddPermission(){
        mainIndex=layer.open({
          type:1,
          title:'添加权限',
          content:$("#addOrUpdateDiv"),
          area:['500px','325px'],
          success:function(index){
            formState = FORM_ADD;
            url=api + "/permission/addPermission";
            //重置表单
            $("#dataFrm")[0].reset();
          }
        });	
      };

      function openUpdatePermission(data) {
        mainIndex=layer.open({
          type:1,
          title:'修改权限',
          content:$("#addOrUpdateDiv"),
          area:['500px','325px'],
          success:function(index){
            formState = FORM_UPDATE;
            url=api + "/permission/updateById";
            //装载数据表格
            form.val("dataFrm",data);
          }
        });	
      };
    });
</script>
<script>

</script>

</body>
</html>